"""
日期相关的操作演示
"""
import datetime
from datetime import datetime, timedelta

from dateutil.relativedelta import *


# 日期格式化

def format_date(date, format):
    return date.strftime(format)


# 计算N天后的日期
def date_add_day(beg_date, days):
    # 字符串转为日期
    delta = timedelta(days=days)
    n_days = beg_date + delta
    return format_date(n_days, "%Y-%m-%d %H:%M:%S")


# 计算两个日期相减的天数
def date_diff(beg_date, end_date):
    """
    返回两个日期之间相隔的天数
    :param beg_date: 起始日期
    :param end_date: 结束日期
    :return: 相隔天数
    """
    tmp = end_date - beg_date
    days = tmp.days
    return tmp.days


def date_add(beg_date, num, date_type):
    """
    根据开始日期，周期计算实际天数
    :param begDate: 原始时间
    :param num:  相加天数
    :param type：相加类型
    :return: 计算后的日期
    """
    tmp_date = beg_date
    ret_date = None
    if date_type == datetime.year:
        ret_date = tmp_date + relativedelta(years=+num)
    elif date_type == datetime.month:
        ret_date = tmp_date + relativedelta(months=+num)
    elif date_type == datetime.day:
        ret_date = tmp_date + relativedelta(days=+num)

    return ret_date


def main():
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    new_date = date_add_day(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 10)
    print(new_date)
    new_date = date_add(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 10, datetime.month)
    print(new_date)
    print(date_diff(datetime.now(), new_date))
